#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/10/22 下午 09:54
# @Author  : wasdzy
# @Site    : 
# @File    : db_utils.py.py
# @Software: PyCharm
import pymysql


def getConnect():
    return pymysql.connect(
        host="localhost",
        user="root",
        password="sa",
        db="test_demo",
        port=3306,
        charset='utf8',
    )


def init_db():
    db = getConnect();
    cursor = db.cursor()
    # cursor.execute('drop table if exists postion_dict')
    # 'positionName': position['positionName'],
    # 'workYear': position['workYear'],
    # 'salary': position['salary'],
    # 'district': position['district'],
    # 'companyFullName': position['companyFullName'],
    sql = """CREATE TABLE postion_dict (
        autoId int PRIMARY KEY auto_increment,
        positionName VARCHAR ( 30 ),
        workYear CHAR ( 10 ),
        salary VARCHAR ( 30 ),
        district VARCHAR ( 30 ),
        companyFullName VARCHAR ( 30 ),
        position_detal VARCHAR (1000),
		createTime CHAR(15)
        )"""
    cursor.execute(sql)
    db.close()


def insert(positionName, workYear, salary, district, companyFullName, position_detal,createTime):
    db = getConnect()
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()
    sql = "insert into postion_dict( positionName,workYear,salary,district,companyFullName,position_detal,createTime) values ('%s', '%s','%s', '%s','%s', '%s','%s')" % (
        positionName, workYear, salary, district, companyFullName, position_detal,createTime)
    try:
        i = cursor.execute(sql)
        db.commit()
        return i
    except Exception as e:
        print(e)
        return 0
    finally:
        db.close()
